<!doctype html>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>商户后台</title>
	<link rel="stylesheet" type="text/css" href="../static/css/basic.css">
	<script src="../static/js/filter.js"></script>
	<style>
		.role-name-add {
			margin-left: 30px;
			vertical-align: bottom;
		}
		.role-remark {
			margin-left: 30px;
			margin-top: 20px;
			width: 190px;
		}
		.role-remark-word {
			vertical-align: center;
		}
		.role-name-error {
			margin-left: 20px;
			color: red;
		}
	</style>
</head>
<body>
	<div class="search">
		<div>
			<span>角色名称</span>
			<input type="text" class="role-name" value="" placeholder="模糊匹配">
		</div>
		<div>
			<button type="button" class="btn btn-default role-menu-search">搜索</button>
		</div>
	</div>
	
	<div class="add-menu">
		<button type="button" class="btn btn-default btn-xs glyphicon glyphicon-plus add-button" data-toggle="modal" data-target="#addRoleModal">创建新角色</button>
	</div>
	<!-- 创建新角色的模态框 -->
	<div class="modal fade" id="addRoleModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-keyboard="false" data-backdrop="static">
	  <div class="modal-dialog" role="document">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="addPowerButton">添加角色</h4>
	      </div>
			
	      <div class="modal-body modal-body-menu">
	      	<div>
			  <span>角色名称: </span>
			  <input type="text" class="role-name-add">
			  <span class="role-name-error"></span>
			</div>
	      	
	      	<div>
			  <span class="role-remark-word">角色描述:</span>
			  <textarea class="role-remark"></textarea>
			  <span class="role-remark-error"></span>
			</div>

			<div class="input-group">
				<span class="role-save-success"></span>
			</div>
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default cancel-menu" data-dismiss="modal">取消</button>
	        <button type="button" class="btn btn-primary save-menu">保存</button>
	      </div>
	    </div>
	  </div>
	</div>
	<!-- 模态框end -->
	<!-- 权限配置模态框 -->
	<div class="edit-role-modal"></div>
	<!-- 权限模态框 end -->
	<div class="menu-table">
		<table>
			<thead class="thead-dark">
				<tr>
					<th>序列号</th>
					<th>角色名称</th>
					<th>角色标识号</th>
					<th>备注</th>
					<th>创建时间</th>
					<th>编辑</th>
				</tr>
			</thead>
			<tbody id="menu_table_body">
			</tbody>
		</table>
	</div>
	<!-- 分页插件 -->
	<div class="cut_page">
		<li>
			每页显示
			<select id="display_count">
				<option value="20">20</option>
				<option value="30">30</option>
				<option value="50">50</option>
				<option value="100">100</option>
			</select>
		</li>
		
		<li class="current_total_page">第<span class="current_page">0</span>/<span class="total_page">0</span>页</li>
		<li class="pre_page">上一页</li>
		<li class="next_page">下一页</li>
		<li class="jump_page">跳转 <input type="text" name="jump_page" value=""> <button type="button" class="btn btn-default">Go</button></li>
	</div>

	<script>
		function getRolePageData() {
			let displayCount = $("#display_count").val();
			let currentPage = $(".current_page").html();
			let totalPage = $(".total_page").html();
			let jumpPage = $(".jump_page input").val();

			let roleName = $(".role-name").val();
			let roleNameAdd = $(".role-name-add").val();
			let roleRemark = $(".role-remark").val();

			return {"displayCount":displayCount, "currentPage":currentPage, "totalPage":totalPage, "jumpPage":jumpPage,
					"roleName":roleName, "roleRemark":roleRemark, "roleNameAdd":roleNameAdd};
		}
		function Ajax(dataJSON, urlHost) {
			$.ajax({
				url: urlHost,
				async:false,
				data: dataJSON,

				success: function(res) {
					if (res.Code == 404) {
						window.parent.location = "/login.html";
						return;
					} 
					if(urlHost.indexOf("/get/role") != -1) {
						showRoleList(res);
					} else if (urlHost.indexOf("/add/role") != -1) {
						addRole(res);
					} else if (urlHost.indexOf("/delete/role") != -1) {
						if (res.Code == 200) {
							Ajax(dataJSON, "/get/role");
						}
					}
				}, 

				error: function() {
					alert("系统异常，请稍后再试");
				}
			});
		}
		
		function deleteRole(roleUid) {
			if (!window.confirm("是否确定删除？")) {
				return;
			}
			let dataJSON = getRolePageData();
			dataJSON["roleUid"] = roleUid;
			Ajax(dataJSON, "/delete/role");
		}
		//当每页显示数更改后，执行的操作
		$("#display_count").change(function() {
			let dataJSON = getRolePageData();
			Ajax(dataJSON, "/get/role");
		});

		//点击上一页的按钮
		$(".pre_page").click(function() {
			let dataJSON = getRolePageData();

			if (dataJSON["currentPage"] == 1) {
				return;
			}
			dataJSON["currentPage"] = parseInt(dataJSON["currentPage"]) - 1;
			Ajax(dataJSON, "/get/role");
		});
	    //点击下一页的按钮时
	    $(".next_page").click(function() {
	 		let dataJSON = getRolePageData();

			if (dataJSON["currentPage"] == dataJSON["totalPage"]) {
				return;
			}

			dataJSON["currentPage"] = parseInt(dataJSON["currentPage"]) + 1;
			Ajax(dataJSON, "/get/role");
	    });
	    //点击跳转那一页的按钮
	    $(".jump_page button").click(function() {
	  		let dataJSON = getRolePageData();

			if (dataJSON["jumpPage"].length <= 0) {
				return;
			}
			Ajax(dataJSON, "/get/role");
	    });
		function addRole(res) {
			let role = $(".role-name-add").val();
			if (res.Code == 200) {
				$(".role-save-success").html(role + "：添加成功");
				$(".cancel-menu").trigger('click');
				Ajax(getRolePageData(), "/get/role");
			} else {
				$(res.Key).html(res.Msg);
			}
		}
		$(".save-menu").click(function() {
			
			let roleName = $(".role-name").val();
			let roleNameAdd = $(".role-name-add").val();
			let roleRemark = $(".role-remark").val();

			$(".role-name-error").html("");
			$(".role-remark-error").html("");

			if (roleNameAdd.length == 0) {
				$(".role-name-error").html("*角色名称不能为空");
			}

			Ajax({"roleName":roleName, "roleRemark":roleRemark, "roleNameAdd":roleNameAdd},"/add/role");
		});
		function showRoleList(res) {
			if (res.Code != 200) {
				alert("获取数据失败");
				return;
			}

			$(".current_page").html(res.CurrentPage);
			$(".total_page").html(res.TotalPage);
			$("#display_count option").each(function() {
				if ($(this).text() == res.DisplayCount) {
					$(this).attr('selected', true);
				}
			});
			$(".jump_page input").val("");

			let str = "";
			for (let i = 0; i < res.RoleInfoList.length; i ++) {
				let v = res.RoleInfoList[i];
				let tmp = "<tr>" + "<th>" + (res.StartIndex+i+1) + "</th>" + 
						  "<th>" + v.RoleName + "</th>" + "<th>" + v.RoleUid + "</th>" + 
						  "<th>" + v.Remark + "</th>" + "<th>" + v.CreateTime + "</th>";
					tmp = tmp + "<th>" + '<div class="btn-group" role="group" aria-label="...">' + 
					// '<button type="button" value="' + v.RoleUid +'" class="btn btn-default" onclick="editRole(this.value);"><span class="glyphicon glyphicon-pencil"></span></button>' +
					'<button type="button" value="' + v.RoleUid +'" class="btn btn-default" onclick="deleteRole(this.value);"><span class="glyphicon glyphicon-trash"></span></button>' + 
					'</div>' + "</th>" + "</tr>";
				str = str + tmp;
			}
			$("#menu_table_body").html(str);
		}
		$(".role-menu-search").click(function() {
			Ajax(getRolePageData(),  "/get/role");
			$(".edit-role-modal").load("/jhboss/edit_role.html");
		});
		$(function() {
            Ajax(getRolePageData(), "/get/role");
            $(".edit-role-modal").load("/jhboss/edit_role.html");
		});
	</script>
</body>
</html>